﻿$(function(){//延迟加载
	var dvbelong='';
	
	var fuxuan="";
	var danxuan='';
	var jId="";
	var jcode='';
	var zf="";
	var sz="";
	var rq='';
	var obj=undefined;
	var zfId='';
	
	$('#search').hide();
	$('#createxcel').hide();
	$('a[name=dvres]').click(function(){
		dvbelong=$(this).attr('value');
		$('#p').load('detaildvsec',{
			dvbelong:dvbelong
		},function(){
			$.getScript('/xasj/js/detaildvsec.js');
		$('input[name=zf]').each(function(){
			var zfval=$(this).data('zfval');
			$(this).textbox();
		});
		$('input[name=szmin]').each(function(){
			$(this).textbox();
		});
		$('input[name=szmax]').each(function(){
			$(this).textbox();
		});
		$('input[name=riqimin]').each(function(){
			$(this).datetimebox();
		});
		$('input[name=riqimax]').each(function(){
			$(this).datetimebox();
		});
		$('select[name=danxuan]').each(function(){
			$(this).combobox();
		});
		
		$('select[name=fuxuan]').each(function(){
			$(this).combobox({
				multiple:true,
			});
		});
		
		});		
		$('#search').show();
		$('#createxcel').show();
		$('#creatallexcel').show();
		$('#dnorth').height('auto');	
	});
	
//	obj={}
	
	$('#search').click(function(){
		getparams();
		$('#searchres').datagrid({
			width:'auto',
			height:'auto',
			//striped:true,		
//			singleSelect:true,
//			url:'searchres',fuxuan='+fuxuan+'&zf='+zf+'&danxuan='+danxuan+'&shuzi='+sz,
			url:'searchres',
			method:'POST',
			queryParams:{
				fuxuan:fuxuan,
				zf:zf,
				danxuan:danxuan,
				shuzi:sz,
				rq:rq,
				dvbelong:dvbelong
			},
			pagination:true,
			rownumbers:true,
			fit:true,			
			columns:obj,
			//onLoadSuccess:function(data){
				//var myOffset = new Object();
				//myOffset.top = 650;
			//}
			onLoadSuccess:function(data){

			}
		});
		var pager = $('#searchres').datagrid('getPager');	// get the pager of datagrid
		pager.pagination({
			buttons:[{
				iconCls:'icon-search',
				width:'150px',
				text:'设备详细信息',
				handler:function(){
					var row=$('#searchres').datagrid('getSelected');
					if($('#'+row.oltId).length>0){alert('已显示该设备');}
					else{
					$('#dig').load('dvdetail',{
						dvbelong:$('a[name=dvres]').attr('value'),
						dvId:row.oltId
					},function(){
						$.getScript('/xasj/js/oltdetail.js');
					});	
					}
				}
			},{
				iconCls:'icon-search',
				width:'150px',
				text:'设备拓扑',
				handler:function(){
					var row=$('#searchres').datagrid('getSelected');
					
				}
			},{
				iconCls:'icon-redo',
				width:'150px',
				text:'执行批操作文件',
				handler:function(){
					var row=$('#searchres').datagrid('getSelected');
					
				}
			}]
		});
	});	
	
	$('#createxcel').click(function(){
		 var rows = $('#searchres').datagrid('getRows');  
           var columns = $("#searchres").datagrid("options").columns[0];  
           var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel   
           var oWB = oXL.Workbooks.Add(); //获取workbook对象   
           var oSheet = oWB.ActiveSheet; //激活当前sheet  
           //设置工作薄名称  
           oSheet.name = "导出Excel报表";  
           //设置表头  
           for (var i = 0; i < columns.length; i++) {  
               oSheet.Cells(1, i+1).value = columns[i].title;  
           }  
           //设置内容部分  
           for (var i = 0; i < rows.length; i++) {  
               //动态获取每一行每一列的数据值  
               for (var j = 0; j < columns.length; j++) {                 
                   oSheet.Cells(i + 2, j+1).value = rows[i][columns[j].field];  
               }     
           }                
           oXL.Visible = true; //设置excel可见属性  
	});
	
	$('#creatallexcel').click(function(){
		getparams();
		$.ajax({
			url:'searchres',
			data:{
				fuxuan:fuxuan,
				zf:zf,
				danxuan:danxuan,
				shuzi:sz,
				rq:rq,
				dvbelong:dvbelong,
				page:1,
				rows:-1
			},
			success:function(data){
				 var rows = data.rows;  
		         var columns =obj[0];
//		         alert(rows);
//		         alert(rows.length);
//		         alert(columns);
		         var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel   
		         var oWB = oXL.Workbooks.Add(); //获取workbook对象   
		         var oSheet = oWB.ActiveSheet; //激活当前sheet  
		         //设置工作薄名称  
		         oSheet.name = "导出Excel报表";  
		         //设置表头  
		         for (var i = 0; i < columns.length; i++) {  
		             oSheet.Cells(1, i+1).value = columns[i].title;  
		         }  
		         //设置内容部分  
		         for (var i = 0; i < rows.length; i++) {  
		             //动态获取每一行每一列的数据值  
		             for (var j = 0; j < columns.length; j++) {                 
		                 oSheet.Cells(i + 2, j+1).value = rows[i][columns[j].field];  
		             }     
		         }                
		         oXL.Visible = true; //设置excel可见属性  
			}
		});
	});
	
	function getparams(){
		fuxuan="";
		danxuan='';
		jId="";
		jcode='';
		zf="";
		sz="";
		rq='';
		zfId='';
		obj=undefined;
		
		var fsl="[[";
		if($('#dajuxiang').combobox('getValue')!=''){
			jId=$('#dajuxiang').combobox('getValue');
			jcode='djxId';
			if($('#juxiang').combobox('getValue')!='请选择'){
				jId=$('#juxiang').combobox('getValue');
				jcode='jxId';
				if($('#jifang').combobox('getValue')!='请选择'){
					jId=$('#jifang').combobox('getValue');
					jcode='jfId';
				}
			}
			danxuan=danxuan+jcode+','+jId;
		}
		
		if($('#dvfirm').combobox('getValue')!=''){
			jId=$('#dvfirm').combobox('getValue');
			jcode='dvfirmId';
			if($('#dvmodel').combobox('getValue')!='请选择'&&$('#dvmodel').combobox('getValue')!=''){
				jId=$('#dvmodel').combobox('getValue');
				jcode='dvmodelId';
			}
			if(danxuan.length==0){
				danxuan=jcode+','+jId;
			}else{
				danxuan=danxuan+':'+jcode+','+jId;
			}		
		}
		
		$('select[textboxname=fuxuan]').each(function(){
			var fuxuanvalues=$(this).combobox('getValues').toString();
			var fufield=$(this).data('fufield');
			if(fuxuanvalues!=''){
				if(fuxuan.length==0){
					fuxuan=fufield+','+fuxuanvalues;
				}else
					fuxuan=fuxuan+":"+fufield+','+fuxuanvalues;
			}
		});
		
		$('select[textboxname=danxuan]').each(function(){
			var danxuanvalues=$(this).combobox('getValues').toString();
			var danfield=$(this).data('danfield');
			if(danxuanvalues!=''){
				if(danxuan.length==0){
					danxuan=danfield+','+danxuanvalues;
				}else
					danxuan=danxuan+":"+danfield+','+danxuanvalues;
			}
		});
		
		$('input[name=zf]').each(function(index){
			zfId=$('input[textboxname=zf]').eq(index).data('zfval');
			if($(this).val()==''){
				
			}else{
			if(zf.length==0){
				zf=zfId+","+$(this).val();
			}else
				zf=zf+":"+zfId+","+$(this).val();
			}
		});
		$('input[name=szmin]').each(function(index){
			szId=$('input[textboxname=szmin]').eq(index).data('szmin');
			var smin=$(this).val();
			var smax=$('input[name=szmax]').eq(index).val();
			if($(this).val()==''&&smax==''){
				
			}else{
			if(smin=='')smin='0';
			if(sz.length==0){
				sz=szId+','+smin+','+smax;
			}else
				sz=sz+':'+szId+','+smin+','+smax;
			}
		});
		
		$('input[textboxname=riqimin]').each(function(index){
			var rqcode=$(this).data('rqmin');
			var rqmin=$(this).datetimebox('getValue');
			var rqmax=$('input[textboxname=riqimax]').eq(index).datetimebox('getValue');
			if(rqmin==''&&rqmax==''){
				
			}else{
			if(rqmin==''){
				rqmin='0';
			}
			if(rq.length==0){
				rq=rqcode+','+rqmin+','+rqmax;
			}else
				rq=rq+';'+rqcode+','+rqmin+','+rqmax;
			}
		});
		
//		fsl=fsl+'{'+'"field"'+':"'+ck+'",'+'"checkbox"'+':"true"}'+',';
		$('input[name=fshow]:checked').each(function(index){
			if($(this).data('ftype')=='3'){
				fsl=fsl+"{field:'"+$(this).val()+"',title:'"+$(this).data('fname')+"',formatter:function(value,row,index){" +
				"if(value!=null&&value!=''){" +
				"var date = new Date(value);" +
				"Y = date.getFullYear() + '-';" +
				"M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';" +
				"D = (date.getDate()< 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';" +
				"h =(date.getHours()< 10 ? '0'+(date.getHours()) : date.getHours()) + ':';" +
				"m =(date.getMinutes()< 10 ? '0'+(date.getMinutes()) : date.getMinutes()) + ':';" +
				"s =(date.getSeconds()< 10 ? '0'+(date.getSeconds()) : date.getSeconds());" +
				" return Y+M+D+h+m+s;}else{return '';}},width"+":'100px'}"+",";
			}else{
				fsl=fsl+"{field:'"+$(this).val()+"',title:'"+$(this).data('fname')+"',width"+":'100px'}"+",";
			}
		});
		fsl=fsl.substring(0,fsl.length-1);
		fsl=fsl+']]';
//		alert(fsl);
		obj=eval(fsl);
	}
	
});